<?php
 /**
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

/**
 * @author api.wes@gmail.com
 * @version 0.1
 */

$currentDir = dirname(__FILE__);
require_once($currentDir . "/sensapilityservice.php");
require_once($currentDir . "/utilities.php");

class SensapilityAfs extends SensapilityService {

  /**
   * @param soapclientnusoap $soapClient the NuSOAP object that communicates 
   * with the service
   * @param array $namespaces associative array that matches namespace label
   * to namespace URIs
   * @param array $defaultNamespaceLabel the namespace to be used with most
   * tags
   * @return none
   */
  function SensapilityAFS($soapClient, $namespaces, $defaultNamespaceLabel) {
    parent::SensapilityService($soapClient, 
                               $namespaces,
                               $defaultNamespaceLabel);
  }
  /**
   * @return SearchResultStyle[] At array of search result style objects that
   * are built into the service, null if the call fails
   */
  function getBuiltinSearchResultStyles(){
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':dummy' => 1);

    return parent::doSoapCall('getBuiltinSearchResultStyles',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              2,
                              true);
  }

  /**
   * @param string $serviceId the publisher id to whose account this search 
   * result style should be saved
   * @param string $name the name to save this search result style as
   * @param string $backgroundC the hexadecimal representation of the 
   * background color for the search results
   * @param string $faintTextC the hexadecimal representation for the faint
   * text color for the search results 
   * @param string $logoBackgroundC the hexadecimal representation of the
   * color that appears as a background for the user defined logo
   * @param string $logoDestUrl the Url the user should be taken to when they
   * click on the user defined logo
   * @param int $logoHeight the height of the logo
   * @param string $logoPlacement where the logo should be placed in the 
   * search box, either 'Top' or 'Left'
   * @param string $logoUrl the address where the logo image can be found
   * @param string $logoWidth the width of the logo image
   * @param string $textC the hexadecimal representation of the text color
   * @param string $visitedUrlC the hexadecimal representation of the color
   * that visited links in the search results should have
   * @return mixed an empty array if the call was successful, null if it failed.
   */
  function saveSearchResultStyle($serviceId,
                                 $name, 
                                 $backgroundC, 
                                 $borderC,
                                 $faintTextC,
                                 $logoBackgroundC,
                                 $logoDestUrl,
                                 $logoHeight,
                                 $logoPlacement,
                                 $logoUrl,
                                 $logoWidth,
                                 $textC,
                                 $titleC,
                                 $urlC,
                                 $visitedUrlC) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $logoPlacement = array('value' => $logoPlacement);
    $callParameters = array($ns . ':synServiceId' => $serviceId);
    $styleParams = array($ns . ':backgroundColor' => $backgroundC,
                         $ns . ':borderColor' => $borderC,
                         $ns . ':faintTextColor' => $faintTextC,
                         $ns . ':logoBackgroundColor' => $logoBackgroundC,
                         $ns . ':logoDestUrl' => $logoDestUrl,
                         $ns . ':logoHeight' => $logoHeight,
                         $ns . ':logoPlacement' => array('value' => $logoPlacement),
                         $ns . ':logoUrl' => $logoUrl,
                         $ns . ':logoWidth' => $logoWidth,
                         $ns . ':name' => $name,
                         $ns . ':textColor' => $textC,
                         $ns . ':titleColor' => $titleC,
                         $ns . ':urlColor' => $urlC,
                         $ns . ':visitedUrlColor' => $visitedUrlC);
    $callParameters['searchResultStyle'] = array("value" => $styleParams);

    return parent::doSoapCall('saveSearchResultStyle',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }


  /**
   * @param string $serviceId the publisher id of the account where this
   * style should be saved
   * @param SearchResultStyle[] $styles an array of 
   * <a href="http://code.google.com/apis/adsense/developer/SearchResultStyle.html">SearchResultStyle</a>
   * objects to save
   * @return mixed an empty array on call success, null on failure 
   */
  function saveSearchResultStyles($serviceId, $styles) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':synServiceId' => $serviceId);
    $callParameters = ArrayToXml($callParameters);

    $styleKeys = array_keys($styles);
    foreach ( $styleKeys as $styleIdx ) {
      $styleObj = $styles[$styleIdx];
      $logoPlacement = array('value' => $styleObj['logoPlacement']);
      $styleParams = array($ns . ':backgroundColor' => 
                               $styleObj['backgroundColor'],
                           $ns . ':borderColor' => $styleObj['borderColor'],
                           $ns . ':faintTextColor' => 
                               $styleObj['faintTextColor'],
                           $ns . ':logoBackgroundColor' => 
                               $styleObj['logoBackgroundColor'],
                           $ns . ':logoDestUrl' => $styleObj['logoDestUrl'],
                           $ns . ':logoHeight' => $styleObj['logoHeight'],
                           $ns . ':logoPlacement' => array('value' => $logoPlacement),
                           $ns . ':logoUrl' => $styleObj['logoUrl'],
                           $ns . ':logoWidth' => $styleObj['logoWidth'],
                           $ns . ':name' => $styleObj['name'],
                           $ns . ':textColor' => $styleObj['textColor'],
                           $ns . ':titleColor' => $styleObj['titleColor'],
                           $ns . ':urlColor' => $styleObj['urlColor'],
                           $ns . ':visitedUrlColor' => 
                               $styleObj['visitedUrlColor']);
      $tmpArray = array('searchResultStyles' => 
                            array("value" => $styleParams));
      $callParameters .= ArrayToXml($tmpArray);
    }

    return parent::doSoapCall('saveSearchResultStyles',
                              $callParameters,
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }

  /**
   * @param string $serviceId the publisher id corresponding to the account
   * to retrieve styles for
   * @return SearchResultStyle[] an array of 
   * <a href="http://code.google.com/apis/adsense/developer/SearchResultStyle.html">SearchResultStyle</a>
   * objects
   */
  function getSearchResultStyles($serviceId) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':synServiceId' => $serviceId);

    return parent::doSoapCall('getSearchResultStyles',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              2,
                              true);
  }

  /**
   * @param string $serviceId the publisher id for the account to delete the
   * style from
   * @param string $name the name of the style to delete
   * @return mixed an empty array if the call is successful, null if it fails
   */
  function deleteSearchResultStyle($serviceId, $name) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = array($ns . ':synServiceId' => $serviceId,
                            $ns . ':name' => $name);

    return parent::doSoapCall('deleteSearchResultStyle',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }

   /**
   * @param string $serviceId the publisher id for the account to delete the
   * style from
   * @param string[] $names the name(s) of the style(s) to delete
   * @return mixed an empty array if the call is successful, null if it fails
   */
  function deleteSearchResultStyles($serviceId, $names) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':synServiceId' => $serviceId);
    $callParameters = ArrayToXml($callParameters);

    $keys = array_keys($names);
    foreach ( $keys as $key ) {
      $tmpArray = array($ns . ':names' => $names[$key]);
      $callParameters .= ArrayToXml($tmpArray);
    }

    return parent::doSoapCall('deleteSearchResultStyles',
                              $callParameters,
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }
  /**
   * @param string $serviceId the publisher id of the account to generate this
   * code for
   * @param string $country the 
   * <a href="http://code.google.com/apis/adsense/developer/adsense_api_languages.html">supported country code</a>
   * where this search box should be geared to
   * @param string $searchType whether this is a GoogleSearch or 
   * GoogleSiteSearch
   * @param array $siteProperties associative array of values consisting of 
   * language and encoding.  language is the language for the Button whereas 
   * encoding is the encoding of the site, set encoding to "" to use default 
   * value of encoding, ISO-8859-1.  Described
   * {@link http://code.google.com/apis/adsense/developer/SiteProperties.html here}
   * @param array $searchOptions associative array of values consisting of 
   * isSafeSearch, which can be set to true to turn on safe search, and 
   * showResultsInNewWindow, which can be set to true to pop up new window 
   * when searching. Described
   * {@link http://code.google.com/apis/adsense/developer/SearchOptions.html here}
   * @param string[] $domains if a site search is done, the list of domains
   * the site search should be performed against
   * @param string $selectedDomain the default domain to use with a 
   * GoogleSiteSearch
   * @param array $searchBoxStyle associative array consisting of style 
   * options described
   * {@link http://code.google.com/apis/adsense/developer/SearchBoxStyle.html here}
   * @param string $channelName the name of the channel this search box
   * should be associated with
   * @return string the code that will generate the AFS search box with the
   * specified parameters
   */
  function generateSearchBoxCode($serviceId, $country, $searchType,
                                 $siteProperties, $searchOptions, $domains,
                                 $selectedDomain, $searchBoxStyle,
                                 $channelName) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $siteProps = array($ns . ':locale' => $siteProperties['locale'],
                       $ns . ':encoding' => $siteProperties['encoding']);
    $searchOpts = array($ns . ':isSafeSearch' => 
                          ConvertToBooleanString($searchOptions['isSafeSearch']),
                        $ns . ':showResultsInNewWindow' => 
                          ConvertToBooleanString($searchOptions['showResultInNewWindow']));

    $boxStyle = array($ns . ':logoType' => array('value' => 
                                            array('value' => 
                                              $searchBoxStyle['logoType'])),
                      $ns . ':isLogoAbove' => 
                        ConvertToBooleanString($searchBoxStyle['isLogoAbove']),
                      $ns . ':isButtonBelow' => 
                        ConvertToBooleanString($searchBoxStyle['isButtonBelow']),
                      $ns . ':backgroundColor' => $searchBoxStyle['backgroundColor'],
                      $ns . ':textColor' => $searchBoxStyle['textColor'],
                      $ns . ':textBoxLength' => $searchBoxStyle['textBoxLength'],
                      $ns . ':isCustomStyle' => 
                        ConvertToBooleanString($searchBoxStyle['isCustomStyle']),
                      $ns . ':searchStyleName' => $searchBoxStyle['searchStyleName']);
    $callParameters = array();
    $callParameters[$ns . ':synServiceId'] = $serviceId;
    $callParameters[$ns . ':country'] = $country;
    $callParameters[$ns . ':searchType'] = array('value' => 
                                            array('value' => $searchType));
    $callParameters[$ns . ':siteProperties'] = 
        array('value' => $siteProps);
    $callParameters[$ns . ':searchOptions'] = array('value' => $searchOpts);

    if ( is_array($domains) ) {
      $keys = array_keys($domains);
      $padding = "";
      foreach ($keys as $key) {
        $callParameters[$ns . ':domains' . $padding] = $domains[$key];
        $padding .= " ";
      }
    }

    $callParameters[$ns . ':selectedDomain'] = $selectedDomain;
    $callParameters[$ns . ':searchBoxStyle'] = array('value' => $boxStyle);
    $callParameters[$ns . ':channelName'] = $channelName;

    return parent::doSoapCall('generateSearchBoxCode',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              0,
                              true);
  }

  /**
   * Get channels
   * @param string $serviceId the publisher id of the account to generate this
   * code for
   * @param string $type whether we should get Custom or URL channels
   * @return ChannelService_Data[] 
   * {@link http://code.google.com/apis/adsense/developer/ChannelService_Data.html ChannelService_Data objects}
   * representing the channels for this service
   */
  function getChannels($serviceId, $type) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = false;
    $callParameters = array($ns . ':synServiceId' => $serviceId,
                            $ns . ':type' => array('value' =>
                                               array('value' => $type)));

    return parent::doSoapCall('getChannels',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              3,
                              true);
  }

  /**
   * Get channels with a certain status
   * @param string $serviceId the publisher id of the account to generate this
   * code for
   * @param string $type whether we should get Custom or URL channels
   * @param string $requestedStatus either active or inactive
   * @return ChannelService_Data[] 
   * {@link http://code.google.com/apis/adsense/developer/ChannelService_Data.html ChannelService_Data objects}
   * representing the channels for this service
   */
  function _getChannelsWithStatus($serviceId, $type, $requestedStatus) {
    $channels = $this->getChannels($serviceId, $type);

    if ( !is_null($channels) ) {
      $keys = array_keys($channels);
      $rtnValue = array();
      foreach ($keys as $key) {
        if ($channels[$key]['status']['value'] == $requestedStatus) {
          $rtnValue[$key] = $channels[$key];
        }
      }
      return $rtnValue;
    } else { 
      return $channels;
    }
  }

  /**
   * Get the inactive channels
   * @param string $serviceId the publisher id of the account to generate this
   * code for
   * @param string $type whether we should get Custom or URL channels
   * @return ChannelService_Data[] 
   * {@link http://code.google.com/apis/adsense/developer/ChannelService_Data.html ChannelService_Data objects}
   * representing the channels for this service
   */
  function getInactiveChannels($serviceId, $type) {
    return $this->_getChannelsWithStatus($serviceId, $type, 'Inactive');
  }

  /**
   * Get the active channels
   * @param string $serviceId the publisher id of the account to generate this
   * code for
   * @param string $type whether we should get Custom or URL channels
   * @return ChannelService_Data[] 
   * {@link http://code.google.com/apis/adsense/developer/ChannelService_Data.html ChannelService_Data objects}
   * representing the channels for this service
   */
  function getActiveChannels($serviceId, $type) {
    return $this->_getChannelsWithStatus($serviceId, $type, 'Active');
  }
}
?>
